package com.yy.controller;


import com.yy.pojo.Article;
import com.yy.pojo.PageBean;
import com.yy.pojo.Result;
import com.yy.pojo.vo.ArticleVo;
import com.yy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
//新增文章
    @Autowired
    private ArticleService articleService;
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }
//更新文章
    @PutMapping
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success();
    }
//查询文章
    @GetMapping
    public Result<PageBean<ArticleVo>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state)
    {
        PageBean<ArticleVo> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }
//删除文章
    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        articleService.deleteArticle(id);
        return Result.success();
    }

    /**
     * 获取所有文章
     * @param categoryId
     * @param state
     * @param title
     * @return
     */
    @GetMapping("/list")
    public Result<List<ArticleVo>> listAll(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String title){
        List<ArticleVo> pb = articleService.listAll(categoryId,state,title);
        return Result.success(pb);
    }
}
